package engine.scene.tree;

import static org.lwjgl.opengl.GL11.*;

import java.util.ArrayList;

import engine.gameObject.GameObject;
import engine.scene.Frustum;

public class Leaf extends Node{

	private ArrayList<GameObject> staticObjects; 
	
	public Leaf(Frustum frustum, double[] min, double[] max) {
		super(frustum, min, max);
		
		staticObjects = new  ArrayList<GameObject>();
	}

	@Override
	public void render(final float timeElapsed) {
//		draw();
		if(getFrustum().cubeInFrustum(getMin()[0], getMin()[1], getMin()[2], getMax()[0], getMax()[1], getMax()[2])) {
			for(GameObject s: staticObjects) {
//				s.lateUpdateAbs(timeElapsed);
				s.renderAbs(timeElapsed);
			}
		}
	}

	@Override
	public void insert(GameObject staticObject) {
		staticObjects.add(staticObject);
	}
	
	public void draw() {
		glColor3f(1, 0, 0);
		glBegin(GL_LINES);
		{
			//front
			glVertex3d(getMin()[0], getMin()[1], getMin()[2]);
			glVertex3d(getMin()[0], getMax()[1], getMin()[2]);
			
			glVertex3d(getMin()[0], getMax()[1], getMin()[2]);
			glVertex3d(getMax()[0], getMax()[1], getMin()[2]);
			
			glVertex3d(getMax()[0], getMax()[1], getMin()[2]);
			glVertex3d(getMax()[0], getMin()[1], getMin()[2]);
			
			glVertex3d(getMax()[0], getMin()[1], getMin()[2]);
			glVertex3d(getMin()[0], getMin()[1], getMin()[2]);
		
			//middle
			glVertex3d(getMin()[0], getMin()[1], getMin()[2]);
			glVertex3d(getMin()[0], getMin()[1], getMax()[2]);
			
			glVertex3d(getMin()[0], getMax()[1], getMin()[2]);
			glVertex3d(getMin()[0], getMax()[1], getMax()[2]);
			
			glVertex3d(getMax()[0], getMax()[1], getMin()[2]);
			glVertex3d(getMax()[0], getMax()[1], getMax()[2]);
			
			glVertex3d(getMax()[0], getMin()[1], getMin()[2]);
			glVertex3d(getMax()[0], getMin()[1], getMax()[2]);
			
			//back
			glVertex3d(getMin()[0], getMin()[1], getMax()[2]);
			glVertex3d(getMin()[0], getMax()[1], getMax()[2]);
			
			glVertex3d(getMin()[0], getMax()[1], getMax()[2]);
			glVertex3d(getMax()[0], getMax()[1], getMax()[2]);
			
			glVertex3d(getMax()[0], getMax()[1], getMax()[2]);
			glVertex3d(getMax()[0], getMin()[1], getMax()[2]);
			
			glVertex3d(getMax()[0], getMin()[1], getMax()[2]);
			glVertex3d(getMin()[0], getMin()[1], getMax()[2]);
		}
		glEnd();
	}

	@Override
	public void reset() {
		for(GameObject s: staticObjects) {
			s.setRendered(false);
		}
	}
	
	@Override
	public void getNearObjects(ArrayList<GameObject> objects, GameObject g) {
		objects.addAll(staticObjects);
	}
	
	public ArrayList<GameObject> getStaticObjects() {
		return staticObjects;
	}
}
